package com.diandi.signature.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.diandi.signature.enums.platfrom.ContractTypeAndTempName;
import com.diandi.signature.url.CreateTradeNo;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 *  用于签章<根据模板类型来签章>
 * @author mwz
 * @create 2017-08-23 13:11
 * @version V1.0.0
 */
public class FddContractSignatureTools {

    public static Map<String, Object> genContractParamMap(String contractType, String params){
        Map<String, Object> returnMap = new HashMap<String, Object>();

        if (contractType.equals(ContractTypeAndTempName.借款协议.getType())){
            returnMap = genLoanMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.信息咨询与管理服务协议.getType())){
            returnMap = genInforServiceMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款人承诺书.getType())){
            returnMap = genLetterOfCommitmentMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.债券转让协议.getType())){
            returnMap = genBondTransferMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.授权委托协议.getType())){
            returnMap = genDelegationMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.委托代扣协议_循环贷.getType())){
            returnMap = genEntrustWithHoldMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.信息咨询与管理服务协议_包年服务.getType())){
            returnMap = packYear(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议并委托代扣协议.getType())){
            returnMap = genLoanAndEntrustWithHoldMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.委托划款授权书_投资人.getType())){
            returnMap = genLetterOfAuthorizationMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_High购房.getType())){
            returnMap = genLoanOnlinMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.信息咨询与管理服务协议_线上.getType())){
            returnMap = genInforServiceOnLineMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_有用分期.getType())){
            returnMap = genLoanByStagesMAp(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.借款协议_High酒店.getType())){
            returnMap = genLoanHighHotalMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_High游艇.getType())){
            returnMap = genLoanHighYachtMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_秒借.getType())){
            returnMap = genLoanMiaoJieMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_苹果专享宝.getType())){
            returnMap = genLoanAppleMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_车贷宝.getType())){
            returnMap = genLoanCurrencyMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_企业机构.getType())){
            returnMap = genLoanEnterMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.委托代偿协议.getType())){
            returnMap = genLoanServiceMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.车信宝借款协议.getType())){
            returnMap = genCheXinBaoMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.定期宝借款协议.getType())){
            returnMap = genRegularLoanMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.活期宝.getType())) {
            returnMap = genHuoQiBaoLoanMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.代偿协议.getType())){
            returnMap = genCompensationAgreementMap(params);
            return returnMap;
        }

        returnMap.put("responseCode", "QZ_7007");
        returnMap.put("responseMsg", "合同类型不合法");

        return returnMap;
    }

    private static Map genCompensationAgreementMap(String params){
        Map returnMap = null;
        Map paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "borrowerName", "borrowerIdNo", "borrowerPhone", "penatlyRate",
                "compensatoryName", "compensatoryIdNo", "compensatoryPhone","totalAmountLower","totalAmoutUpper","rate","afterdays",
                "aCustomerId", "bCustomerId", "aSignDate", "bSignDate");
        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M28"));
        }

        return paramMap;
    }

    private static Map genHuoQiBaoLoanMap(String params) {
        Map returnMap = null;
        Map paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "bName", "bIdNo","apr","interestDay",
                "amount", "minAmount", "maxAmount","numDay","bCustomerId","platCustomerId","signDate");
        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M26"));
        }
        return paramMap;
    }

    /**
     * 生成定期宝借款协议参数
     * @param params
     * @return
     */
    private static Map genRegularLoanMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M25"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成车信宝借款协议参数
     * @param params
     * @return
     */
    private static Map genCheXinBaoMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M24"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    private static Map genLoanServiceMap(String params) {

        Map returnMap = null;
        Map paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "aName", "aIdNo", "bName","num","penaltyRate",
                "bIdNo", "loanPrincipleCaptal", "loanPrincipleLower","loanDays","loanStartDay","loanEndDay","feeCapital","feeLower",
                "aCustomerId","bCustomerId","aSignDate","bSignDate");
        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M21"));
        }
        return paramMap;
    }

    /**
     * 生成借款协议_企业机构参数
     * @param params
     * @return
     */
    private static Map genLoanEnterMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M18"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }


    /**
     * 生成借款协议_车贷宝参数
     * @param params
     * @return
     */
    private static Map genLoanCurrencyMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M17"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成借款协议_苹果专享宝参数
     * @param params
     * @return
     */
    private static Map genLoanAppleMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M16"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }


    /**
     * 生成借款协议_秒借参数
     * @param params
     * @return
     */
    private static Map genLoanMiaoJieMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M15"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }


    /**
     * 生成借款协议_High游艇参数
     * @param params
     * @return
     */
    private static Map genLoanHighYachtMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M14"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成借款协议_High酒店参数
     * @param params
     * @return
     */
    private static Map genLoanHighHotalMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M13"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成借款协议有用分期参数
     * @param params
     * @return
     */
    private static Map genLoanByStagesMAp(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr","sex",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M12"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成 信息咨询与管理服务协议_线上参数
     * @param params
     * @return
     */
    private static Map genInforServiceOnLineMap(String params) {
        Map returnMap = null;
        Map paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "borrowerName", "identType", "borrowerIdNo",
                "feeLower", "borrowerCustomerId", "signDate");
        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M11"));
        }
        return paramMap;
    }

    /**
     * 生成借款协议_High购房参数
     * @param params
     * @return
     */
    private static Map genLoanOnlinMap(String params) {

        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr",
                "borrowerID", "borrowerName", "borrowerCustomerId", "lenderInfoList","borrowerIdentType",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal","totalRepayPrincipal","totalRepayInsterest",
                "toatlRepayRepay","totalLastAmount","feeLower");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest", "lenderSignDate",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M10"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成委托划款授权书_投资人参数
     * @param params
     * @return
     */
    private static Map<String,Object> genLetterOfAuthorizationMap(String params) {
        Map returnMap = new HashMap();
        Map paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "realName", "idCard", "userName", "userId",
                "thirdUserId", "aCustomerId", "aSignDate");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M9"));
        }

        return paramMap;
    }

    /**
     * 生成债券转让协议参数
     * @param params
     * @return
     */
    public static Map genBondTransferMap(String params){
        Map returnMap = null;

        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        Map<String, String> paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "assignorList", "assigneeName", "assigneeIdNo",
                "assigneeAddress", "assigneePhone", "obligorName", "obligorIdNo", "bondAmount", "bondAmountCapital", "loanDays",
                "loanStartDate", "loanEndDate", "assigneeCustomerId", "assigneeSignDate");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray assignorArray = JSONArray.parseArray(paramMap.get("assignorList"));
        List<Map<String, String>> assignorList = new ArrayList<Map<String, String>>();

        for (Object assignor : assignorArray) {
            Map assignorMap = string2Map(assignor.toString());
            returnMap = CheckParams.checkParamsIsExist(assignorMap, "name", "idenType", "identNo", "customerId", "signDate");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            assignorList.add(assignorMap);
        }

        Set<String> keys = paramMap.keySet();
        for (String key : keys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMapObj.containsKey("contractNo") || StringUtils.isBlank(paramMapObj.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M4"));
        }

        paramMapObj.put("assignorList", assignorList);

        return paramMapObj;
    }

    /**
     * 生成授权委托协议参数
     * @param params
     * @return
     */
    public static Map genDelegationMap(String params){
        Map returnMap = new HashMap();
        Map paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "licensorName", "licensorIdNo",  "licensorPhone",
                "licenseeName", "licenseeIdNo",  "licenseePhone","licensorCustomerId", "licenseeCustomerId",
                "licensorDate", "licenseeDate");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M5"));
        }

        return paramMap;
    }

    /**
     * 生成委托代扣协议参数
     * @param params
     * @return
     */
    public static Map genEntrustWithHoldMap(String params){
        Map returnMap = new HashMap();
        Map paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "bName", "bIdNo", "bCustomerId", "aSignDate", "bSignDate");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }
        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M6"));
        }

        return paramMap;
    }

    /**
     * 生成信息咨询与管理服务协议参数
     * @param params
     * @return
     */
    public static Map genInforServiceMap(String params){
        Map returnMap = null;
        Map paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "borrowerName", "borrowerIdNo", "borrowerAddress",
                "borrowerPhone", "feeCapital", "feeLower", "borrowerCustomerId", "signDate");
        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M2"));
        }
        return paramMap;
    }

    /**
     * 生成借款人承诺书参数
     * @param params
     * @return
     */
    public static Map genLetterOfCommitmentMap(String params){
        Map returnMap = null;
        Map paramMap = string2Map(params);
        returnMap = CheckParams.checkParamsIsExist(paramMap, "borrowerName", "borrowerIdNo",
                "borrowerPhone", "borrowerCustomerId", "signDate");
        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M3"));
        }

        return paramMap;
    }


    /**
     * 生成借款协议参数
     * @param params
     * @return
     */
    public static Map genLoanMap(String params){
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = string2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        returnMap = CheckParams.checkParamsIsExist(paramMap, "apr", "borrowerAddress",
                "borrowerID", "borrowerName", "borrowerPhone", "borrowerCustomerId", "borrowerSignDate", "lenderInfoList",
                "loanDays", "loanEndDay", "loanPrincipleCaptal", "loanPrincipleLower", "loanStartDay", "repayDetailList", "repayInsterest",
                "repayType", "totalInterest", "totalLoanAmount", "totalPrincipal");

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        for (Object lenderInfo : lenderInfoArray) {
            lenderInfoMap = string2Map(lenderInfo.toString());
            returnMap = CheckParams.checkParamsIsExist(lenderInfoMap, "identNo", "identType", "interest", "lenderSignDate",
                    "lenderCustomerId", "loanAmount", "name", "principal");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            lenderInfoList.add(lenderInfoMap);
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        for (Object repayDetail : repayDetailArray) {
            repayDetailMap = string2Map(repayDetail.toString());
            returnMap = CheckParams.checkParamsIsExist(repayDetailMap, "lastAmount", "repayDate", "repayInsterest", "repayPrincipal", "totalRepay");
            if (returnMap != null && !returnMap.isEmpty()){
                return returnMap;
            }
            repayDetailList.add(repayDetailMap);
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M1"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    public static Map packYear(String params){
        Map<String, String> returnMap = null;
        Map paramMap = string2Map(params);
       /* returnMap = CheckParams.checkParamsIsExist(paramMap, "borrowerName", "borrowerID", "borrowerAddress",
                "borrowerPhone", "customerId", "signDate","yearNo","num","loanDays","feeCapital","feeLower","type");*/
       returnMap = CheckParams.checkParamsIsExist(paramMap, "borrowerName", "borrowerIdNo", "borrowerPhone", "cardType", "timeLimit",
               "overdueTimesLimit", "overdueDaysLimit", "serviceCharge", "borrowerCustomerId", "signDate");
        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M7"));
        }
        return paramMap;
    }

    public static Map genLoanAndEntrustWithHoldMap(String params){
        Map genEntrustWithHoldMap = genEntrustWithHoldMap(params);
        if (genEntrustWithHoldMap.containsKey("responseCode")){
            return genEntrustWithHoldMap;
        }
        Map genLoanMap = genLoanMap(params);

        return genLoanMap;
    }

    public static Map string2Map(String param){

        param = param.replaceAll("\"", "");

        param = param.replaceAll("\\{", "{\"").replaceAll("}", "\"}").
                replaceAll("=", "\"=\"").replaceAll(",", "\",\"").replaceAll(":", "\":\"");

        Map map = JSONObject.parseObject(param.replaceAll(":","：").replaceAll("=", ":").replaceAll("&", ""));

        return map;
    }
}